import AMapLoader from "@amap/amap-jsapi-loader";
let _AMap = null;
let mapObj = null;
let marker = null;

export default {
    initMap(ele, longitude, latitude){
        AMapLoader.load({
            "key": "5c26d1d52c6047079f2329260cbe0851", // old 5c26d1d52c6047079f2329260cbe0851 new 20f4dfb0f9ca79f47260e08de6a09773
            "version": "1.4.15", // 2.0 JSAPI版本，缺省时默认为 1.4.15
            "plugins": ["AMap.Geolocation", "AMap.Geocoder", "AMap.Autocomplete", "AMap.PlaceSearch", "AMap.MoveAnimation"]
        }).then((AMap) => {
            _AMap = AMap;
            mapObj = new AMap.Map(ele, {
                zoom: 15,
                // mapStyle: "amap://styles/blue",
                center: [longitude, latitude]
            });
            this.addMarker(longitude, latitude);
        }).catch(e => {
            console.log("amapAction-error----->", e);
        });
    },
    addMarker(longitude, latitude){
        marker = new _AMap.Marker({
            position: new _AMap.LngLat(longitude, latitude),
        })
        mapObj.add(marker);
    },
    removeMarker(){
        mapObj.remove(marker);
    },
    markerMoveTo(longitude, latitude){
        mapObj.setCenter([longitude, latitude], {
            duration: 800,
            delay: 500,
        });
        this.removeMarker();
        this.addMarker(longitude, latitude);
        /*marker.moveTo([longitude, latitude], {
            duration: 800,
            delay: 500,
        });*/
    },
    mapSearch(keyword){
        return new Promise((resolve) => {
            _AMap.plugin('AMap.Autocomplete', function(){
                let autoComplete= new _AMap.Autocomplete({city: '淮安'});
                autoComplete.search(keyword, function(status, result) {
                    resolve(result.tips);
                });
            });
        });
    }
}
